<?php
require_once 'Hamlap/Base.php';
abstract class Hamlap_Html_Element_Base extends Hamlap_Base
{
	/**
	 * The default indentation level
	 * @var integer
	 */
	protected $_level = 0;

	/**
	 * The default tab size in spaces
	 * @var integer
	 */
	protected $_tab_size = 4;

	/**
	 * Array with children of this element (including text)
	 * @var array
	 */
	protected $_children = array();

	/**
	 * Render the children of this element as HTML
	 * @return string
	 */
	public function renderChildren()
	{
		$output = '';
		if(count($this->_children))
			foreach($this->_children as $child)
				$output.= (is_object($child)) ? "\n".$child->getIndentation().$child : $this->getIndentation().$child;
		return str_replace("\n\n", "\n", $output);
	}

	/**
	 * Add a new child to this element
	 * @param string|Hamlap_Html_Element_Base $element
	 * @return Hamlap_Html_Element_Base
	 */
	public function addChild($element)
	{
		$this->_children[] = $element;
		return $this;
	}

	/**
	 * Set indentation level for this element
	 * @param integer $level The level of the current element
	 * @param integer $tab_size The size of a single tab in spaces
	 * @return Hamlap_Html_Element_Base
	 */
	public function setLevel($level = 0, $tab_size = 4)
	{
		$this->_level = intval($level);
		$this->_tab_size = intval($tab_size);
		return $this;
	}

	/**
	 * Get the indentation level of this element
	 * @return integer The indentation level of this element
	 */
	public function getLevel()
	{
		return $this->_level;
	}

	/**
	 * Get the whitespace for the current indentation level
	 * @return string
	 */
	protected function getIndentation()
	{
		return str_repeat(' ', ($this->_level * $this->_tab_size));
	}
}